package ferran.tomas.ta;

import ferran.tomas.dominio.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity {
	private ServidorPrincipal sp;
	private EditText et1;
	private EditText et2;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		et1 = (EditText) findViewById(R.id.editName);
		et2 = (EditText) findViewById(R.id.editPass);

		sp = ServidorPrincipal.getInstancia();
	}

	public void acces(View V) {
		String name = et1.getText().toString();
		String pass = et2.getText().toString();

		if (sp.conectarServidorPrincipal(name, pass)) {
			User u = User.getInstancia();
			u.setNombre(name);
			u.setContactos(utilsXML.getContactos(sp.getDocRespuestaServidorPrincipal()));
			Intent intent = new Intent(Login.this, MainMenu.class);
			Login.this.startActivity(intent);
		} else {
			// Mostrar mensaje de error
			String error = sp.getRespuestaServidorPrincipal();
			Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT)
					.show();
		}
	}

	// option menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		menu.setGroupVisible(R.id.grupo2, false);
		menu.setGroupVisible(R.id.grupo3, false);
		menu.setGroupVisible(R.id.grupo4, false);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection

		switch (item.getItemId()) {

		case R.id.language:
			language();
			return true;
		case R.id.help:
			// gohelp();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void language() {
		Intent intent = new Intent(Login.this, Language.class);
		Login.this.startActivity(intent);
	}

	// public void help() {
	// Intent intent = new Intent(Login.this, Help.class);
	// Login.this.startActivity(intent);
	// }
}